//
//  TipViewController.m
//  QYer
//
//  Created by lanou on 16/8/11.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "TipViewController.h"
#import "TipModel.h"
@interface TipViewController () <UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, assign) BOOL isTrue;
@end

@implementation TipViewController



- (NSMutableArray *)titleArray
{
    if (!_titleArray)
    {
        _titleArray = [[NSMutableArray alloc] init];
    }
    return _titleArray;
}



- (NSMutableArray *)urlArray
{
    if (!_urlArray)
    {
        _urlArray = [[NSMutableArray alloc] init];
    }
    return _urlArray;
}









- (void)createSubViews
{
    // 创建label
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 30)];
    _label.text = [NSString stringWithFormat:@"1 / %ld", self.titleArray.count];
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_label];
    
    
    // 创建滚动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94, kScreenWidth, kScreenHeight - 94)];
    _scrollView.contentSize = CGSizeMake(kScreenWidth * self.titleArray.count, 0);
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];

    
    // 创建webView
    for (NSInteger i = 0; i < self.titleArray.count; i++)
    {
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight)];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.urlArray[i]]]];
        
        NSLog(@"%@", self.titleArray[i]);
        [webView loadRequest:request];
        
        [_scrollView addSubview:webView];
    }
    
    //NSLog(@"AAAAA:%@", self.titleArray.firstObject);
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%@", self.titleArray.firstObject] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction)];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 120, 64, 120, 0) style:(UITableViewStylePlain)];
    _tableView.scrollEnabled = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];

    
}



- (void)rightAction
{
    self.tableView.frame = CGRectMake(self.view.frame.size.width - 120, 64, 120, 0);
    if(_isTrue == NO)
    {
        [UIView animateWithDuration:0.5 animations:^{
            self.tableView.frame = CGRectMake(self.view.frame.size.width - 120, 64, 120, self.titleArray.count * 45);
        }];
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            self.tableView.frame = CGRectMake(self.view.frame.size.width - 120, 64, 120, 0);
        }];
        
    }
    
    _isTrue = !_isTrue;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = YES;
    [self createSubViews];

    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.backgroundColor = [UIColor cyanColor];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.scrollView setContentOffset:CGPointMake(indexPath.row * kScreenWidth, 0) animated:YES];
    
    self.label.text = [NSString stringWithFormat:@"%ld / %ld", indexPath.row + 1, self.titleArray.count];
    
    [self.navigationItem.rightBarButtonItem setTitle:self.titleArray[indexPath.row]];
    
    

}





- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获得屏幕的宽度, 也就是scrollView在屏幕上的宽度
    CGFloat width = scrollView.frame.size.width;
    
    // 获得偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 获得当前所在位置的索引
    NSInteger index = offsetX / width;
    
    self.label.text = [NSString stringWithFormat:@"%ld / %ld", index + 1, self.titleArray.count];

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
